1007A - Reorder the Array - CodeForces Solution


combinatorics data structures math sortings two pointers *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
/*----------------------------------------------------------------------*/
/*||||    ||| ||||||||||| ||||||||||| |||||||||||  |||||||||  |||      |||
  |||||   |||     |||         |||         |||      |||        |||      |||
  ||||||| |||     |||         |||         |||      |||        |||      |||
  ||| |||||||     |||         |||         |||      |||||||||  ||||||||||||
  |||   |||||     |||         |||         |||            |||  |||      |||
  |||    ||||     |||         |||         |||            |||  |||      |||
  |||    |||| |||||||||||     |||     |||||||||||  |||||||||  |||      |||*/
/*--------------------------Defiination----------------------------------*/
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<string, string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vii;
typedef vector<long long> vl;
typedef vector<vl> vvl;
/*--------------------------------values----------------------------------*/
double eps = 1e-9;
int inf = 1000000005;
long long infl = 1000000000000000005LL;
double PI = acos(-1);
int dirx[8] = {-1, 0, 0, 1, -1, -1, 1, 1};
int diry[8] = {0, 1, -1, 0, -1, 1, -1, 1};
/*--------------------------------Driver code-------------------------------*/
#define Author_NitishBharat \
  int t;                    \
  cin >> t;                 \
  while (t--)
#define _coding_is_fun_             \
  ios_base::sync_with_stdio(false); \
  cin.tie(NULL);
// #ifndef ONLINE_JUDGE
// freopen("input_.txt", "r", stdin);
// freopen("output_.txt", "w", stdout);
// #endif
/*--------------------------------function----------------------------------*/

bool odd(int a)
{
  if (a % 2)
    return true;
  else
    return false;
}

#define N 1000000007

long long exponentiation(long long base,
                         long long exp)
{
  if (exp == 0)
    return 1;

  if (exp == 1)
    return base % N;

  long long t = exponentiation(base, exp / 2);
  t = (t * t) % N;

  if (exp % 2 == 0)
    return t;
  else
    return ((base % N) * t) % N;
}
/*--------------------------------macros------------------------------*/
#define iin(n) \
  int n;       \
  cin >> n;
#define out(n) cout << n << endl;
#define outc(n) cout << n << " ";
#define sin(s) \
  string s;    \
  cin >> s;
#define vi(v, n) vector<int> v(n);
#define vin(v, n)   \
  vector<int> v(n); \
  rep(i, n) cin >> v[i];
#define vout(v, n) rep(i, n) outc(v[i]) cout << endl;
#define aout(a, n) rep(i, n) outc(a[i]) cout << endl;
/*---------------------------------------------------------------------*/
#define mapin(m, v) \
  for (auto i : v)  \
  {                 \
    m[i]++;         \
  }                 \
/*---------------------------------------------------------------------*/
#define mapout(m)                \
  for (auto i : m)               \
  {                              \
    outc(i.first) outc(i.second) \
  }                              \
  cout << endl;
/*---------------------------------------------------------------------*/
#define setout(s)                             \
  for (auto i = s.begin(); i != s.end(); i++) \
  {                                           \
    int p = *i;                               \
                cout                          \
            << p << " ";                      \
  }                                           \
  cout << endl;
/*---------------------------------------------------------------------*/
#define mi(m) map<int, int> m;
#define msi(m) map<string, int> m;
#define mci(m) map<char, int> m;
#define mex(mex) int mex = INT_MIN;
#define men(men) int men = INT_MAX;
#define int long long
#define debug(x) cerr << "The value of " << #x << " is " << x << endl;
#define see(x) cout << "The value of " << #x << " is " << x << endl;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = n - 1; i >= 0; i--)
#define sqr(x) ((LL)(x) * (x))
#define reset(a, b) memset(a, b, sizeof(a))
#define fi first
#define se second
#define pb push_back
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define sz(v) (int)v.size()
#define haa cout << "YES" << endl;
#define naa cout << "NO" << endl;
#define permute next_permutation
/*---------------------------------solve-------------------------------*/
void solve()
{


iin(n);
vin(v,n);
sort(all(v));
mi(m);
mapin(m,v);
int cnt=0;
repn(i,n){
            auto it=m.rbegin();
            int p=it->first;
            if(p>v[i]){
                        m[p]--;
                        cnt++;
                        if( m[p]==0)m.erase( p);
            }
}
cout<<cnt<<endl;

}

/*---------------------------------main---------------------------------*/
signed main()
{
  _coding_is_fun_

  solve();
}
/*---------------------------------------------------------------------*/


Comments

Submit
0 Comments
More Questions

1660C - Get an Even String
489B - BerSU Ball
977C - Less or Equal
1505C - Fibonacci Words
1660A - Vasya and Coins
1660E - Matrix and Shifts
1293B - JOE is on TV
1584A - Mathematical Addition
1660B - Vlad and Candies
1472C - Long Jumps
1293D - Aroma's Search
918A - Eleven
1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding